<?php
namespace Bundle\FireIceBundle\Revision;

use Bundle\FireIceBundle\FireIceEngine;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;


class RevisionManager
{
    private $_fireice;

    private $_entity;

    private $_entityHistory;

    public function __construct(FireIceEngine $engine, $entity, $entityHistory)
    {
        $this->_fireice = $engine;
        $this->_entity = $entity;
        $this->_entityHistory = $entityHistory;
    }


    private $updates = array();
    public function getEntityClassName()
    {
        return $this->_entity;
    }

    public function getEntityHistoryClassName()
    {
        return $this->_entityHistory;
    }

    public function getLastVersion()
    {

    }

    public function prePersist(LifecycleEventArgs $args)
    {
        
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        
    }

    public function preUpdate(PreUpdateEventArgs $args)
    {
        return false;

        $history = new \Bundle\FireIceBundle\Entities\DocumentHistory();
        $history->setOldValue($args->getOldValue('field1'));
        $history->setNewValue($args->getNewValue('field1'));
        $history->setUser('USER #' . \rand());
        $history->setAction('Update of entity #' . $args->getEntity()->getId());

        //\var_dump($history);

        $this->updates[] = $history;
        //$args->getEntityManager()->persist($history);



        //\var_dump($args->getEntityChangeSet());
        //\var_dump($args->getEntity());
    }
    public function postUpdate(LifecycleEventArgs $args)
    {
        
    }

    public function persistUpdates()
    {
        foreach($this->updates as $update)
        {
            $this->_fireice->getContainer()->getDoctrine_ORM_EntityManagerService()->persist($update);
        }
        $this->_fireice->getContainer()->getDoctrine_ORM_EntityManagerService()->flush();
    }
}
